
const app = getApp();
Page({
  data: {
    listData: {
      onItemTap: 'handleListItemTap',
      data: [],
      longTap :'handleListLongTap', 
      upper:'upper',
      lower:'lower',
      height:'588px',
      scrollTop:588,
      total:0,
    },
    pageNum:1,
    pageSize:15,
    summList:[],
  },
  upper(){
    console.log("upper");
  },
  lower(){
    var that=this;
    console.log("lower");
    var num =this.data.pageNum+1;

    if(num<=Math.ceil(this.data.total/this.data.pageSize)) {
      this.setData({
        pageNum :num,
      })
      var name='';
      that.selBusinessSummary(name);
     
    }
   

    

  },
  handleListItemTap(e) {
    var index = e.currentTarget.dataset.index;
    var rId = this.data.listData.data[index].id;
    dd.navigateTo({
      url: '../report/report?rId=' + rId,
    })
  },
  selSjList(e){
    var that =this;
    var name =e.detail.value;
    that.setData({
       summList:[],
    })
    that.selBusinessSummary(name);
  },
  selBusinessSummary(name){
    var that =this;
    var isAdmin=  app.globalData.userinfo.isAdmin;  //管理员默认全部可查
    var isBoss= app.globalData.userinfo.isBoss;  //老板全部可查
    var isLeaderInDepts=app.globalData.userinfo.isLeaderInDepts; //是否为部门主管   String类型！
    var isDept =isLeaderInDepts.split(":") //分割字符后 前后有{}
    var Uid = app.globalData.userinfo.userid;
    var url = app.globalData.url;
    var department=app.globalData.userinfo.department;    //部门id
    var pageNum =this.data.pageNum;
    var pageSize =this.data.pageSize;
    var summList =[];
    var summList2 =[];
      dd.httpRequest({ 
            url:url+'/api/BusinessSummary/selBusinessSummary',
            method:'post',
            datatype:'json',
            data:{
                departmentIdString:department,
                isboss:isBoss,
                isDeptString:isDept[1],
                isadmin:isAdmin,
                Uid:Uid,
                name:name,  
                pageNum:pageNum,
                pageSize:pageSize,
            },
            success:function(res){
              if(res.data.rows.length==0){
                that.setData({
                    'listData.data':[],
                })
              }else{
                if(that.data.pageNum==1){
                  res.data.rows.forEach(function (item,index){
                    that.data.summList.push({
                        'title': res.data.rows[index].rName,
                        'arrow': 'horizontal',
                        'extra': res.data.rows[index].rFzr,
                        'id': res.data.rows[index].rId
                    })
                  });
                  that.setData({
                      'listData.data':that.data.summList,
                      total:res.data.total,
                  })
                }else{
                  res.data.rows.forEach(function (item,index){
                    that.data.summList.push({
                        'title': res.data.rows[index].rName,
                        'arrow': 'horizontal',
                        'extra': res.data.rows[index].rFzr,
                        'id': res.data.rows[index].rId
                    })
                  });
                  that.setData({
                      'listData.data':that.data.summList,
                      total:res.data.total,
                  })
                }
              }
              
            } 
     });
  },
 onShow(options) {
    var that =this;
    var name='';
    that.setData({
        summList:[],
        'listData.data':[],
        pageNum:1,
        'listData.total':0,
    })
   
    that.selBusinessSummary(name);

  },
  handleListLongTap(e){
    var that=this;
    var url = app.globalData.url;
    var index = e.currentTarget.dataset.index;
    var rId = this.data.listData.data[index].id;
    var uid = app.globalData.userinfo.userid;
    var weekId = "";
      dd.confirm({
        title: '温馨提示',
        content: '您确认要发起该商机审批流程？',
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        success: function(confirm){
            if(confirm.confirm){
                dd.httpRequest({ 
                  url:url+'/api/BusinessSummary/tiqi_sj',
                  method:'post',
                  datatype:'json',
                  data:{
                    ids:rId,
                    uid:uid,
                  },
                  success:function(res){
                    if(res.data.data>0){
                      dd.showToast({
                        content: '发起审批流程成功！', // 文字内容
                      });
                    }else{
                      dd.showToast({
                        content: '该商机已经审批通过，无需发起！', // 文字内容
                      });
                    }
                  } 
                });
            }
        }
      });
      
  },
  handleListLongTap2(e){
    var that=this;
    var url = app.globalData.url;
    var index = e.currentTarget.dataset.index;
    var rId = this.data.listData.data[index].id;
    var weekId = "";
      dd.confirm({
        title: '温馨提示',
        content: '您确认要删除该商机信息，若删除将删除所有与本条相关的商机周报',
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        success: function(confirm){
            if(confirm.confirm){
                dd.httpRequest({ 
                  url:url+'/api/BusinessReport/deleteReport',
                  method:'post',
                  datatype:'json',
                  data:{
                    rId:rId,
                  },
                  success:function(res){
                    dd.httpRequest({ 
                      url:url+'/api/hr/weekly/list',
                      method:'post',
                      datatype:'json',
                      data:{
                        rid:rId,
                      },
                      success:function(res){
                          for (var i = 0; i < res.data.data.length; i++) {
                            weekId += res.data.data[i].id + ",";
                          }
                          dd.httpRequest({ 
                              url:url+'/api/hr/weekly/remove',
                              method:'post',
                              datatype:'json',
                              data:{
                                ids:weekId,
                              },
                              success:function(res){
                                that.onShow();
                              } 
                            });
                      } 
                    });
                  } 
                });
            }
        }
      });
      
  },
});
